package com.hzdl.car.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Customer;
import com.hzdl.car.bean.User;
import com.hzdl.car.service.CustomerService;
import com.hzdl.car.util.ExcelUtil;
import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.util.ResultObj;
import com.hzdl.car.vo.CustomerVo;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("customer")
public class CustomerController {
    @Resource
    private CustomerService customerService;
    @RequestMapping("findCustomer")
    public HzdlResultJson findCustomer(CustomerVo customerVo){
        PageInfo<Customer> pageInfo=customerService.findCustomer(customerVo);
        return HzdlResultJson.ok(pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("delCustomer")
    private ResultObj delCustomer(CustomerVo customerVo){
        try {
            customerService.delCustomer(customerVo);
            return ResultObj.DELETE_SUCCESS;

        }catch (Exception e){
            return  ResultObj.DELETE_ERROR;
        }
    }
    @RequestMapping("customerDelChk")
    public ResultObj customerDelChk(@RequestParam(value = "ids[]") String[] ids){
        try {
            customerService.customerDelChk(ids);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }
    @RequestMapping("addCustomer")
    public ResultObj addCustomer(CustomerVo customerVo){
        try {

            customerService.addCustomer(customerVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }
    @RequestMapping("updateCustomer")
    public  ResultObj updateCustomer(CustomerVo customerVo){
        try {
            customerService.updateCustomer(customerVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            return ResultObj.UPDATE_ERROR;
        }
    }
    @RequestMapping("findCustomerByExcel")
    public ResponseEntity<Object> findCustomerByExcel(CustomerVo customerVo) {
        String fileName = "客户数据.xls";
        String sheetName = "客户数据";
        try {

            // 查询客户信息内容
            List<Customer> list = customerService.findCustomerExcel(customerVo);
            // 通过工具类获取workbook对象
            ByteArrayOutputStream bos = ExcelUtil.createExcelByCustomer(list, sheetName);
            //处理文件名乱码
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //创建 封装响应头信息的对象
            HttpHeaders headers = new HttpHeaders();
            //封装响应内容类型(APPLICATION_OCTET_STREAM 响应的内容不限定)
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            //设置下载的文件的名称
            headers.setContentDispositionFormData("attachment", fileName);
            return new ResponseEntity<Object>(bos.toByteArray(), headers, HttpStatus.CREATED);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

}
